<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Services\IndexService;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\UploadedFile;

class InfoSetingController extends Controller
{

    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
        return $content->header('信息设置')->description('信息设置')->body($this->form());
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $info = Redis::get('Info');
        $info = json_decode($info, true);


        $defaultSetting = [
            'title'             => $info['title'] ?? '',
            'abbreviation'      => $info['abbreviation'] ?? '',
            'company'           => $info['company'] ?? '',
            'address'           => $info['address'] ?? '',
            'phone'             => $info['phone'] ?? '',
            'mailbox'           => $info['mailbox'] ?? '',
            'image'             => $info['image'] ?? '',
            'wechat_code'       => $info['wechat_code'] ?? '',
            'company_profile'   => $info['company_profile'] ?? '',
            'company_advantage' => $info['company_advantage'] ?? '',
            'company_image'     => $info['company_image'] ?? '',
            'nav_image'         => $info['nav_image'] ?? '',
            'all_model_title'   => $info['all_model_title'] ?? '',
            'all_model_desc'    => $info['all_model_desc'] ?? '',
            'menu_image'        => $info['menu_image'] ?? '',
        ];

        $form = new Form($defaultSetting);

        $form->action('info');

        $form->text('title', '公司名称')->required();
        $form->text('abbreviation', '公司简称')->required();
        $form->text('company', '公司名称(英文)')->required();
        $form->text('phone', '电话')->required();
        $form->email('mailbox', '邮箱')->required();
        $form->text('address', '公司地址')->required();
        $form->textarea('company_profile', '公司简介')->required();
        $form->textarea('company_advantage', '公司优势')->required()->help('每一条用英文+分隔');
        $form->image('company_image', '公司宣传图片')
             ->required()
             ->help('建议尺寸比：4:3(图片大小在线压缩网站：https://imagecompressor.com/zh/)');
        $form->image('nav_image', '导航图片')
             ->required()
             ->help('建议尺寸:1920*500(图片大小在线压缩网站：https://imagecompressor.com/zh/)');
        $form->image('menu_image', '菜单栏图片')->required()->help('建议尺寸:3:4(图片大小在线压缩网站：https://imagecompressor.com/zh/)');
        $form->image('image', '底部“联系我们”图片')->required();
        $form->image('wechat_code', '微信二维码')->required();
        $form->html('<span class="label label-danger">以下是所有模特设置</span><br>');
        $form->text('all_model_title', '所有模特--标题')->required();
        $form->textarea('all_model_desc', '所有模特--描述')->required();

        return $form;
    }


    public function store(Request $request)
    {
        $data = $request->all();
        $info = Redis::get('Info');
        $info = json_decode($info, true);

        if (isset($data['image'])) {
            $data['image'] = $this->getImgUrl($data, 'image');
        } else {
            $data['image'] = $info['image'];
        }

        if (isset($data['company_image'])) {
            $data['company_image'] = $this->getImgUrl($data, 'company_image');
        } else {
            $data['company_image'] = $info['company_image'];
        }

        if (isset($data['nav_image'])) {
            $data['nav_image'] = $this->getImgUrl($data, 'nav_image');
        } else {
            $data['nav_image'] = $info['nav_image'];
        }

        if (isset($data['wechat_code'])) {
            $data['wechat_code'] = $this->getImgUrl($data, 'wechat_code');
        } else {
            $data['wechat_code'] = $info['wechat_code'];
        }

        if (isset($data['menu_image'])) {
            $data['menu_image'] = $this->getImgUrl($data, 'menu_image');
        } else {
            $data['menu_image'] = $info['menu_image'];
        }

        $data = json_encode($data);
        Redis::set('Info', $data);

        IndexService::indexCache();

        admin_toastr('设置成功！');

        return back();
    }

    /**
     * 获取图片七牛云url
     *
     * DateTime: 2019/5/16 14:25
     *
     * @param $data
     * @param $fileKey
     *
     * @return string
     */
    private function getImgUrl($data, $fileKey)
    {
        if (isset($data[$fileKey])) {
            $url = $this->uploadImgToQiNiu($data[$fileKey]);
            if (!$url) {
                $url = '';
                admin_toastr('图片上传失败', 'error');
            }
        } else {
            $redPacketSetting = json_decode(config('red_packet_setting'), true);

            $url = isset($redPacketSetting[$fileKey]) ? $redPacketSetting[$fileKey] : '';
        }

        return $url;
    }

    /**
     * 上传图片到七牛云
     *
     * DateTime: 2019/5/16 11:07
     *
     * @param UploadedFile $file
     *
     * @return false|string
     */
    private function uploadImgToQiNiu(UploadedFile $file)
    {
        $storage    = Storage::disk(config('admin.upload.disk'));
        $uniqueName = md5(uniqid()) . '.' . $file->getClientOriginalExtension();

        return $storage->putFileAs(config('admin.upload.directory.image'), $file, $uniqueName);
    }
}
